const util = require('./util.js')
const baseurl = util.baseurl

export { getVisitList, addVisit, updateVisit, getVisitDetail, uploadVisitFile, deleteVisitFile, getVisitFileUrl };

// 获取探访列表
const getVisitList = (data) => {
    return new Promise((resolve, reject) => {
        const userInfo = wx.getStorageSync('userInfo')
        // console.log(userInfo)
        wx.request({
            url: baseurl + '/Visit/SelectVisitRecord',
            method: 'POST',
            header: {
                'content-type': 'application/json',
                'Username': userInfo.username,
                'Authorization': userInfo.token,
                'Userrole': userInfo.role,
                'Uid': userInfo.userid
            },
            data: data,
            success: (res) => {
                resolve(res.data)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}
//添加探访
const addVisit = (data) => {
    return new Promise((resolve, reject) => {
        const userInfo = wx.getStorageSync('userInfo')
        // console.log(userInfo)
        wx.request({
            url: baseurl + '/Visit/EnrollVisit',
            method: 'POST',
            header: {
                'content-type': 'application/json',
                'Username': userInfo.username,
                'Authorization': userInfo.token,
                'Userrole': userInfo.role,
                'Uid': userInfo.userid
            },
            data: data,
            success: (res) => {
                resolve(res.data)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}
//上传探访文件
const uploadVisitFile = (data) => {
    return new Promise((resolve, reject) => {
        const userInfo = wx.getStorageSync('userInfo')
        // console.log(userInfo)
        wx.uploadFile({
            url: baseurl + '/Visit/AddVisitFile',
            filePath: data.File,
            name: 'file',
            formData: {
                'FileName': data.FileName,
                'VisitUid': data.VisitUid
            },
            header: {
                'content-type': 'multipart/form-data',
                'Username': userInfo.username,
                'Authorization': userInfo.token,
                'Userrole': userInfo.role,
                'Uid': userInfo.userid
            },
            success: (res) => {
                resolve(res.data)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}
//下载探访文件
const getVisitFileUrl = (data) => {
    return new Promise((resolve, reject) => {
        const userInfo = wx.getStorageSync('userInfo')
        wx.request({
            url: baseurl + '/Visit/GetVisitFileFromUrl',
            method: 'GET',
            header: {
                'content-type': 'application/json',
                'Username': userInfo.username,
                'Authorization': userInfo.token,
                'Userrole': userInfo.role,
                'Uid': userInfo.userid
            },
            data: data,
            success: (res) => {
                resolve(res.data)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}
//删除探访文件
const deleteVisitFile = (data) => {
    return new Promise((resolve, reject) => {
        const userInfo = wx.getStorageSync('userInfo')
        // console.log(userInfo)
        wx.request({
            url: baseurl + '/Visit/DeleteVisitFile',
            method: 'GET',
            header: {
                'content-type': 'application/json',
                'Username': userInfo.username,
                'Authorization': userInfo.token,
                'Userrole': userInfo.role,
                'Uid': userInfo.userid
            },
            data: data,
            success: (res) => {
                resolve(res.data)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}
//更新探访
const updateVisit = (data) => {
    return new Promise((resolve, reject) => {
        const userInfo = wx.getStorageSync('userInfo')
        // console.log(userInfo)
        wx.request({
            url: baseurl + '/Visit/UpdateVisit',
            method: 'POST',
            header: {
                'content-type': 'application/json',
                'Username': userInfo.username,
                'Authorization': userInfo.token,
                'Userrole': userInfo.role,
                'Uid': userInfo.userid
            },
            data: data,
            success: (res) => {
                resolve(res.data)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}
//获取探访详情
const getVisitDetail = (id) => {
    return new Promise((resolve, reject) => {
        const userInfo = wx.getStorageSync('userInfo')
        wx.request({
            url: baseurl + '/Visit/GetVisitRecordByUid',
            method: 'GET',
            header: {
                'content-type': 'application/x-www-form-urlencoded',
                'Username': userInfo.username,
                'Authorization': userInfo.token,
                'Userrole': userInfo.role,
                'Uid': userInfo.userid
            },
            data: {
                "uid": id
            },
            success: (res) => {
                resolve(res.data)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}

// 获取职工列表
// const getStaffList = (data) => {
//     return new Promise((resolve, reject) => {
//         const userInfo = wx.getStorageSync('userInfo')
//         // console.log(userInfo)
//         wx.request({
//             url: baseurl + '/Staff/GetStaffList',
//             method: 'POST',
//             header: {
//                 'content-type': 'application/json',
//                 'Username': userInfo.username,
//                 'Authorization': userInfo.token,
//                 'Userrole': userInfo.role,
//                 'Uid': userInfo.userid
//             },
//             data: data,
//             success: (res) => {
//                 resolve(res.data)
//             },
//             fail: (err) => {
//                 reject(err)
//             }
//         })
//     })
// }

